package org.dromara.manu.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.manu.domain.ManuKpiRecord;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.manu.domain.vo.ManuKpiRecordVo;
import org.dromara.manu.domain.bo.ManuKpiRecordBo;
import org.dromara.manu.service.IManuKpiRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 记录
 *
 * @author Bin Liu
 * @date 2024-08-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manu/kpiRecord")
public class ManuKpiRecordController extends BaseController {

    private final  IManuKpiRecordService manuKpiRecordService;

    /**
     * 查询记录列表
     */
    @SaCheckPermission("manu:kpiRecord:list")
    @GetMapping("/list")
    public TableDataInfo<ManuKpiRecordVo> list(ManuKpiRecordBo bo, PageQuery pageQuery) {
        return manuKpiRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出记录列表
     */
    @SaCheckPermission("manu:kpiRecord:export")
    @Log(title = "记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ManuKpiRecordBo bo, HttpServletResponse response) {
        List<ManuKpiRecordVo> list = manuKpiRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "记录", ManuKpiRecordVo.class, response);
    }

    /**
     * 获取记录详细信息
     *
     * @param id 主键
     *
     * R为返回体
     */
    @SaCheckPermission("manu:kpiRecord:query")
    @GetMapping("/{id}")
    public R<ManuKpiRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(manuKpiRecordService.queryById(id));
    }

    /**
     * 新增记录
     */
    @SaCheckPermission("manu:kpiRecord:add")
    @Log(title = "记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ManuKpiRecordBo bo) {
        return toAjax(manuKpiRecordService.insertByBo(bo));
    }

    /**
     * 修改记录
     */
    @SaCheckPermission("manu:kpiRecord:edit")
    @Log(title = "记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ManuKpiRecordBo bo) {
        return toAjax(manuKpiRecordService.updateByBo(bo));
    }

    /**
     * 删除记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manu:kpiRecord:remove")
    @Log(title = "记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(manuKpiRecordService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 设为默认值
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manu:kpiRecord:edit")
    @PostMapping("/setDefault")
    public R<Void> setDefault(@NotEmpty(message = "主键不能为空")
                              @RequestBody Long[] ids) {
        return toAjax(manuKpiRecordService.setDefault(ids));
    }

    @SaIgnore
    @GetMapping("/dppboard")
    public TableDataInfo<ManuKpiRecord> getDppBoard(){
        String area = "bv";
        Integer year = 2024;
        List<ManuKpiRecord> dataList = manuKpiRecordService.getAnnualData(year, area);
        TableDataInfo<ManuKpiRecord> tableDataInfo = new TableDataInfo<>();
        // 将数据设置到TableDataInfo中，build方法
        tableDataInfo.build(dataList);
        return tableDataInfo;
    }

}
